﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.GoodsDelivery;

namespace ERPGoodsDelivery.Delivery
{
    public partial class FrmBaseInfo : Form
    {
        //属性
        private static FrmBaseInfo _instance;
        public static FrmBaseInfo GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmBaseInfo();
                }
                return _instance;
            }
            set
            {
                _instance = value;
            }
        }

        //参数
        private BaseInfoBLL bll = new BaseInfoBLL();


        public FrmBaseInfo()
        {
            InitializeComponent();
        }

        private void FrmBaseInfo_Load(object sender, EventArgs e)
        {
            List<BaseInfoBLL.StationInfo> stations = bll.GetStationInfo();
            BindStation(stations);
            List<BaseInfoBLL.StationGoodsInfo> stationGoods = bll.GetStationGoodsInfo();
            BindStationGoods(stationGoods);
            List<BaseInfoBLL.ReceiveStation> receiveStations = bll.GetReceiveStation();
            BindReceiveStation(receiveStations);
        }

        //Enter键新增客运站信息
        private void txtDeliverObject_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyData == Keys.Enter)
                {
                    Cursor = Cursors.WaitCursor;
                    InsertStation();
                    List<BaseInfoBLL.StationInfo> stations = bll.GetStationInfo();
                    BindStation(stations);
                    Cursor = Cursors.Default;
                }
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        //新增客运站信息
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                InsertStation();
                List<BaseInfoBLL.StationInfo> stations = bll.GetStationInfo();
                BindStation(stations);
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //Enter键新增货运站及其电话信息
        private void txtStationRemark_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyData == Keys.Enter)
                {
                    Cursor = Cursors.WaitCursor;
                    InsertStationGoods();
                    List<BaseInfoBLL.StationGoodsInfo> stationGoods = bll.GetStationGoodsInfo();
                    BindStationGoods(stationGoods);
                    Cursor = Cursors.Default;
                }
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        //新增货运站及其电话信息
        private void btnAdd2_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                InsertStationGoods();
                List<BaseInfoBLL.StationGoodsInfo> stationGoods = bll.GetStationGoodsInfo();
                BindStationGoods(stationGoods);
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //Enter键新增收货站及其电话
        private void txtReceiveStation_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyData == Keys.Enter)
                {
                    Cursor = Cursors.WaitCursor;
                    InsertReceiveStation();
                    List<BaseInfoBLL.ReceiveStation> stations = bll.GetReceiveStation();
                    BindReceiveStation(stations);
                    Cursor = Cursors.Default;
                }
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //新增收货站及其电话
        private void btnAdd3_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                InsertReceiveStation();
                List<BaseInfoBLL.ReceiveStation> stations = bll.GetReceiveStation();
                BindReceiveStation(stations);
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //删除客运站
        private void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                if (lbDeliverObjects.SelectedItems == null || lbDeliverObjects.SelectedItems.Count == 0)
                {
                    MessageBox.Show("没有客运站被选中！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (DialogResult.Yes == MessageBox.Show("确定要删除选中的货运站吗？","确认",MessageBoxButtons.YesNo,MessageBoxIcon.Warning))
                {
                    Cursor = Cursors.WaitCursor;
                    DelStation();
                    List<BaseInfoBLL.StationInfo> stations = bll.GetStationInfo();
                    BindStation(stations);
                    Cursor = Cursors.Default;
                }
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //删除货运站及其电话
        private void btnDel2_Click(object sender, EventArgs e)
        {
            try
            {
                if (lbStationRemarks.SelectedItems == null || lbStationRemarks.SelectedItems.Count == 0)
                {
                    MessageBox.Show("没有货运站被选中！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (DialogResult.Yes == MessageBox.Show("确定要删除选中的货运站吗？","确认",MessageBoxButtons.YesNo,MessageBoxIcon.Warning))
                {
                    Cursor = Cursors.WaitCursor;
                    DelStationGoods();
                    List<BaseInfoBLL.StationGoodsInfo> stationGoods = bll.GetStationGoodsInfo();
                    BindStationGoods(stationGoods);
                    Cursor = Cursors.Default;
                }
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //删除收货站及其电话
        private void btnDel3_Click(object sender, EventArgs e)
        {
            try
            {
                if (lbReceiveStation.SelectedItems == null || lbReceiveStation.SelectedItems.Count == 0)
                {
                    MessageBox.Show("没有收货站被选中！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (DialogResult.Yes == MessageBox.Show("确定要删除选中的货运站吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                {
                    Cursor = Cursors.WaitCursor;
                    DelReceiveStation();
                    List<BaseInfoBLL.ReceiveStation> stations = bll.GetReceiveStation();
                    BindReceiveStation(stations);
                    Cursor = Cursors.Default;
                }
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //------------------------公共方法--------------------------
        /// <summary>
        /// 绑定货运站
        /// </summary>
        /// <param name="stations"></param>
        private void BindStation(List<BaseInfoBLL.StationInfo> stations)
        {
            try
            {
                lbDeliverObjects.DataSource = null;
                lbDeliverObjects.ValueMember = "StationId";
                lbDeliverObjects.DisplayMember = "StationName";
                lbDeliverObjects.DataSource = stations;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 绑定客运站
        /// </summary>
        /// <param name="stationGoods"></param>
        private void BindStationGoods(List<BaseInfoBLL.StationGoodsInfo> stationGoods)
        {
            try
            {
                lbStationRemarks.DataSource = null;
                lbStationRemarks.ValueMember = "stationGoodsId";
                lbStationRemarks.DisplayMember = "stationGoodsNamePhone";
                lbStationRemarks.DataSource = stationGoods;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 绑定收货站
        /// </summary>
        /// <param name="receiveStations"></param>
        private void BindReceiveStation(List<BaseInfoBLL.ReceiveStation> receiveStations)
        {
            try
            {
                lbReceiveStation.DataSource = null;
                lbReceiveStation.ValueMember = "ReceiveStationId";
                lbReceiveStation.DisplayMember = "StationNamePhone";
                lbReceiveStation.DataSource = receiveStations;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 新增客运站信息
        /// </summary>
        private void InsertStation()
        {
            try
            {
                if (txtDeliverObject.Text.Trim().Length == 0)
                {
                    MessageBox.Show("客运站名不能为空！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                string stationName = txtDeliverObject.Text.Trim();
                List<BaseInfoBLL.StationInfo> stations = lbDeliverObjects.DataSource as List<BaseInfoBLL.StationInfo>;
                foreach (BaseInfoBLL.StationInfo s in stations)
                {
                    if (stationName == s.StationName)
                    {
                        MessageBox.Show("客运站已经存在！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                bll.InsertStation(stationName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 新增货运站及其电话信息
        /// </summary>
        private void InsertStationGoods()
        {
            try
            {
                if (txtStationRemark.Text == null || txtStationRemark.Text.Trim().Length == 0)
                {
                    MessageBox.Show("货运站及其电话不能为空！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                string stationGoodsNamePhone = txtStationRemark.Text.Trim();
                List<BaseInfoBLL.StationGoodsInfo> stations = lbStationRemarks.DataSource as List<BaseInfoBLL.StationGoodsInfo>;
                foreach (BaseInfoBLL.StationGoodsInfo s in stations)
                {
                    if (stationGoodsNamePhone == s.StationGoodsNamePhone)
                    {
                        MessageBox.Show("货运站及其电话已经存在！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                bll.InsertStationGoods(stationGoodsNamePhone);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void InsertReceiveStation()
        {
            try
            {
                if (txtReceiveStation.Text == null || txtReceiveStation.Text.Trim().Length == 0)
                {
                    MessageBox.Show("收货站及其电话不能为空！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                string receiveStation = txtReceiveStation.Text.Trim();
                List<BaseInfoBLL.ReceiveStation> stations = lbReceiveStation.DataSource as List<BaseInfoBLL.ReceiveStation>;
                foreach (BaseInfoBLL.ReceiveStation s in stations)
                {
                    if (receiveStation == s.StationNamePhone)
                    {
                        MessageBox.Show("收货站及其电话已经存在！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                bll.InsertReceiveStation(receiveStation);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 删除客运站
        /// </summary>
        private void DelStation()
        {
            try
            {
                List<int> stationIds = new List<int>();
                ListBox.SelectedObjectCollection items = lbDeliverObjects.SelectedItems;
                foreach (var item in items)
                {
                    BaseInfoBLL.StationInfo s = item as BaseInfoBLL.StationInfo;
                    stationIds.Add(s.StationId);
                }
                bll.DelStation(stationIds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 删除货运站及其电话
        /// </summary>
        private void DelStationGoods()
        {
            try
            {
                List<int> stationGoodsIds = new List<int>();
                ListBox.SelectedObjectCollection items = lbStationRemarks.SelectedItems;
                foreach (var item in items)
                {
                    BaseInfoBLL.StationGoodsInfo s = item as BaseInfoBLL.StationGoodsInfo;
                    stationGoodsIds.Add(s.StationGoodsId);
                }
                bll.DelStationGoods(stationGoodsIds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region 删除收货站及其电话
        /// <summary>
        /// 删除收货站及其电话
        /// </summary>
        private void DelReceiveStation()
        {
            try
            {
                List<int> receiveStationIds = new List<int>();
                ListBox.SelectedObjectCollection items = lbReceiveStation.SelectedItems;
                foreach (var item in items)
                {
                    BaseInfoBLL.ReceiveStation s = item as BaseInfoBLL.ReceiveStation;
                    receiveStationIds.Add(s.ReceiveStationId);
                }
                bll.DelReceiveStation(receiveStationIds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
